
*Filename: 3_child-parent-merge.do
*Created: 20201016
*Last edited: 20201104

/*Description: 

	This file uses the cleaned individual-level income and demographic data to
	first create two intermediate datasets "potenitalmothers.dta" where all 
	variables have a m_ prefix and a "potentialfathers.dta" where all variables
	have a f_ prefix. These are not actual mothers and fathers, but all individuals 
	with their income and demographic data recorded with the m_ or f_ prefix.
	Then we take the full individual-clean file, merge in the cleaned child-parent-linked-IDs
	dataset. Now we merge in the potentialmothers.dta on the unique mother ID to
	get mothers' data linked to the child, and merge in potentialfathers.dta on
	the unique father ID to get fathers' data linked to the child as well.
*/

******************************************************* 
clear 
//version 16.0
set more off
capture log close 
set maxvar 32000

*Log file
log using ${projdata}/child-parent-merge.log, replace


**************************
*** Get potential parent data

*Tempfile for potential "mothers"
clear
use ${projdata}/individual-clean.dta
	renvars, prefix(m_)
save ${projdata}/potentialmothers.dta, replace
	
*Tempfile for potential "fathers"
clear
use ${projdata}/individual-clean.dta
	renvars, prefix(f_)
save ${projdata}/potentialfathers.dta, replace


**************************** 
*** Merge parent data into individual-level data
clear
use ${projdata}/individual-clean.dta
merge 1:1 newid using ${projdata}/child-parent-linked-IDs.dta, gen(merge_parents)
drop if merge_parents==2	


*Merge in data for mothers
merge m:1 m_newid using ${projdata}/potentialmothers.dta, gen(m_merge)
drop if m_merge==2 // drop "potential mother" records that were not linked to any individual's mother ID
tab m_merge

*Merge in data for fathers
merge m:1 f_newid using ${projdata}/potentialfathers.dta, gen(f_merge)
drop if f_merge==2 // drop "potential mother" records that were not linked to any individual's father ID
tab f_merge


*SAVE merged child-parent file (this keeps records not linked to parents or to parents' data)
qui compress
save ${projdata}/child-parent.dta, replace


*ERASE intermediate potential "mothers" and "fathers" files
cd ${projdata}
erase potentialmothers.dta
erase potentialfathers.dta

clear
log close

*End 3_child-parent-merge.do*

